import numpy as np
import matplotlib.pyplot as plt
import matplotlib.pyplot as plt
import numpy as np
import matplotlib
from PIL import Image as PILImage
import io
data="""YSBS20255b-2009-1  0.155   0.1536  0.162725172
YSBS20255b-2009-2  0.43    0.4327  0.458406131
YSBS20255b-2009-3  0.67    0.6538  0.692641388
YSBS20255b-2009-5  0.0028  0.0027  0.002860403
YSBS20255b-2009-6  0.142   0.1394  0.147681568
YSBS20255b-2009-7  0.664   0.6183  0.65503238
GSB03-2453-2008-2  0.256   0.2409  0.255211548
GSB03-2453-2008-4  0.085   0.0849  0.089943796
YSBS11279-2000  0.803   0.7395  0.783432711
9902-GBW(E)010137   1.21    1.1099  1.175837682
Q235-YSBS11173a-2007    0.188   0.1942  0.205737164
16MnV5-YSBC35201-97-211 0.171   0.1646  0.174378667
Q345b-YSBS11264a-2011   0.158   0.1593  0.168763801
YSBS11078d-2012    0.0018  0.0016  0.001695054
BS1020 0.21    0.2138  0.226501573
GCr15-YSBS11273b-2007   0.999   0.962   1.01915114"""
data=data.split("\n")
for i in range(len(data)):
    data[i]=data[i].split(" ")
    data[i] = list(filter(lambda x: x!="", data[i]))
std=[]
measure=[]
yfited=[]
for i in range(len(data)):
    std.append(float(data[i][1]))
    measure.append(float(data[i][2]))
    yfited.append(float(data[i][3]))
print(measure,std,yfited)
matplotlib.use('qtAGG')

class DraggableRectangle:
    def __init__(self, rect):
        self.rect = rect
        self.press = None
    def connect(self):
        'connect to all the events we need'
        print(dir(self.rect),self.rect)
        self.cidpress = self.rect.figure.canvas.mpl_connect(
            'button_press_event', self.on_press)
        self.cidrelease = self.rect.figure.canvas.mpl_connect(
            'button_release_event', self.on_release)
        # self.cidmotion = self.rect.figure.canvas.mpl_connect(
        #     'motion_notify_event', self.on_motion)
    def on_press(self, event):
        'on button press we will see if the mouse is over us and store some data'
        if event.inaxes != self.rect.axes: return
        contains, attrd = self.rect.contains(event)
        if not contains: return
        print(dir(self.rect),self.rect)
        print('event contains', self.rect._x,self.rect._y)
        x0=self.rect._x
        y0 =self.rect._y
        print("x0",x0)
        print("y0",y0)
        print("xdata",event.xdata)
        print("ydata",event.ydata)
        print(self.rect.get_data())
        self.press = x0, y0, event.xdata, event.ydata
    # def on_motion(self, event):
    #     'on motion we will move the rect if the mouse is over us'
    #     if self.press is None: return
    #     if event.inaxes != self.rect.axes: return
    #     x0, y0, xpress, ypress = self.press
    #     dx = event.xdata - xpress
    #     dy = event.ydata - ypress
    #     #print('x0=%f, xpress=%f, event.xdata=%f, dx=%f, x0+dx=%f' %
    #     #      (x0, xpress, event.xdata, dx, x0+dx))
    #     self.rect.set_x(x0+dx)
    #     self.rect.set_y(y0+dy)
    #     self.rect.figure.canvas.draw()
    def on_release(self, event):
        'on release we reset the press data'
        self.press = None
        self.rect.figure.canvas.draw()
    def disconnect(self):
        'disconnect all the stored connection ids'
        self.rect.figure.canvas.mpl_disconnect(self.cidpress)
        self.rect.figure.canvas.mpl_disconnect(self.cidrelease)
        self.rect.figure.canvas.mpl_disconnect(self.cidmotion)
fig, ax = plt.subplots()  # Create a figure containing a single axes.
rects=ax.scatter(measure, std,c="red")  # Plot some data on the axes.
ax.plot(measure,yfited)        
# fig = plt.figure()
# ax = fig.add_subplot(111)
# rects = ax.bar(range(10), 20*np.random.rand(10))
drs = []
print(dir(rects),rects)
print(rects.get_paths())
for rect in rects.get_paths():
    dr = DraggableRectangle(rect)
    dr.connect()
    drs.append(dr)
plt.show()